﻿@model Huirui.Cavan.Presentation.ViewModel.StockModule.IStockTypeViewModel
@{
    var window_name = "addStockTypeWindow" + Model.Purpose;
    var tableid = "addStockType" + Model.Purpose;
}
<div id="@window_name" class="easyui-window" data-options="onOpen:function(){  $('#addStockTypeTable').datagrid({url: '/stocktype/list'});},closed:true,minimizable:false,maximizable:false,collapsible:false,title:'@Html.Raw("增加存货类别")',modal:true" style="width: 600px; height: 500px; padding: 2px;">
    @using (Html.BeginForm(ActionConst.ADD, ControllerConst.STOCKTYPE, FormMethod.Post, new { id = "StockType" + Model.Purpose }))
    { 
        <div class="tool_bar">
            <input id="addStockType_save" type="submit" style="display: none;" />
            <input id="addStockType_reset" type="reset" style="display: none" />
            <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#addStockType_save').click()">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" id="addStockTypeReset">重置</a>
            <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'" id="addStockType_query">查询</a>
        </div>
        @Html.Hidden("cmb_name_for_addstocktype", Model.CmbName);
        @Html.Hidden("wd_name_for_addstocktype", window_name);
        <table class="form_heard">
            <tr>
                <td class="c160">
                    <label>存货类别名称:</label>
                </td>
                <td class="c160">
                    @Html.TextBoxFor(m => m.Item.Name, new { id = "addStockTypeName" })
                    @Html.TextBoxFor(m => m.Item.Id, new { id = "addstocktype_id", style = "display: none;" })
                </td>
            </tr>
        </table>    
    }
    <div id="StockTypetb">
        <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" id="deleteStockType">选择删除</a>
    </div>
    <table id="addStockTypeTable">
    </table>
</div>
<script type="text/javascript">
    $(function ()
    {
        var select_list_name = $("input[name ='cmb_name_for_addstocktype']")[0].value;
        var win_name = $("input[name ='wd_name_for_addstocktype']")[0].value;
        $("form[action^='/stocktype/add']").submit(function ()
        {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data.IsSuccess)
                    {
                        $(select_list_name + ' option').each(function ()
                        {
                            if ($(this).val() == "0" || $(this).val() == data.Messages[1])
                            {
                                $(this).remove();
                            }
                        });
                        $("<option value='" + data.Messages[1] + "'>" + data.Messages[2] + "</option>").appendTo($(select_list_name));
                        $("<option value='0'>[添加...]</option>").appendTo($(select_list_name));
                        $(select_list_name).attr("value", data.Messages[1]);
                        closeWindow(win_name);
                    }
                    else
                    {
                        alert(data.Messages[0]);
                    }

                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $('#addStockTypeTable').datagrid({
            class: 'easyui-datagrid',
            toolbar: '#StockTypetb',
            title: "存货类型",
            idField: 'Id',
            singleSelect: 'true',
            fitColumns: true,
            frozenColumns: [[{ field: 'IsSelected', checkbox: true}]],
            columns: [[
              { field: 'Id', title: '代码', width: 60 },
              { field: 'Name', title: '名称', width: 100 },
              { field: 'ModifyDate', title: '修改日期', width: 80 },
              { field: 'CreateDate', title: '创建日期', width: 80 }
            ]],
            onSelect: function (rowIndex, rowData)
            {
                $("#addStockTypeName").val(rowData.Name);
                $("#addstocktype_id").val(rowData.Id);
            }
        });

        $("#deleteStockType").bind("click", function ()
        {
            if ($('#addStockTypeTable').datagrid('getChecked').length)
            {
                var selected = $('#addStockTypeTable').datagrid('getChecked');

                var effectRow = new Object();
                if (selected.length)
                {
                    effectRow["selected"] = JSON.stringify(selected);
                }

                $.post("/stocktype/delete", effectRow, function (data)
                {
                    if (data.IsSuccess)
                    {
                        $('#addStockTypeTable').datagrid('reload');
                        $('#addStockTypeTable').datagrid('clearChecked');
                        $(select_list_name + ' option').each(function ()
                        {
                            if ($(this).val() == data.Messages[1])
                            {
                                $(this).remove();
                            }
                        });
                        closeWindow(win_name);
                    }
                }, "JSON").error(function ()
                {
                    $.messager.alert("提示", "网络异常，请稍候再试");
                });
            }
        });

        $("#addStockTypeReset").bind("click", function ()
        {
            $('#addStockType_reset').click();
            $('#addStockTypeTable').datagrid('uncheckAll');
        });

        $("#addStockType_query").bind("click", function ()
        {
            $('#addStockTypeTable').datagrid({
                url: "/stocktype/search?stocktypename=" + escape($("#addStockTypeName").val())
            });
        });

       
    });    
</script>
